<?php

namespace app\model;

use app\stateData;
use think\Model;

class SystemCertificate extends Model
{
    /**
     * 证书表
     */

    /**
     * @return void
     * 递归查询父类id，不存在则添加
     * @param $name
     * @param $type
     */
    public function recursionId($name,$type): void
    {

    }

    /**
     * 获取证书对应id
     * @param string $name
     * @param int $type
     * @param int $pid
     * @return mixed
     */
    public function getCertificateIdModel(string $name, int $type,int $pid=0)
    {
        return $this->where(['certificate_name'=>$name,'certificate_type'=>$type,'parent_id'=>$pid])->value('id');
    }

    /**
     * 添加证书
     * @param string $name
     * @param int $type
     * @param int $pid
     * @return SystemCertificate|Model
     */

    public function addCertificateModel(string $name, int $type, int $pid)
    {
        return self::create([
            'certificate_name' => $name,
            'certificate_type' =>$type,
            'parent_id'=>$pid
        ]);
    }


    /**
     * 修改证书数据
     * @param $data
     * @return SystemCertificate
     */
    public function editCertificateModel($data)
    {
        return self::update($data);
    }

    /**
     * 删除证书
     * @param array $arr
     * @return bool
     */
    public function delCertificateModel(array $arr)
    {

        return self::destroy($arr);
    }

    /**
     * 查询证书列表
     * @return SystemCertificate[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCertificateListModel($type=4)
    {
        return $this->where(['is_k'=>0])->select();
    }

    public function getCertificateCountModel($type=4)
    {
        return $this->where('certificate_type','<',$type)->count();
    }
    /**
     * 查询该id是否有子类
     * @param $id
     * @return SystemCertificate[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function getIdSubClass($id):array
    {

        return $this->where(['parent_id'=>$id])->select()->toArray ();
    }

    public function getSpeciality($pid,$where,$page=1)
    {
        return $this->where (['parent_id'=>$pid])->whereLike ('certificate_name','%'.$where.'%')->page($page,stateData::$_page)->select ();
    }

    public function getSpecialityCount($pid)
    {
        return $this->where (['parent_id'=>$pid])->count();
    }

    /**
     * 证书名称获取证书id
     * @param $name
     * @return mixed
     */
    public function getWhereCertificateIdModel($name,$pid='')
    {
        if ($pid){
            $id = $this->where (['certificate_name'=>$name,'parent_id'=>$pid])->value ('id');
        }else{
            $id = $this->where (['certificate_name'=>$name])->value ('id');
        }

        if ($id) return $id;
        return '';

    }

    /**
     * 通过证书id查询证书名称
     * @param $name
     * @return mixed
     */
    public function getWhereIdCertificateModel($name){
        return $this->where (['id'=>$name])->value ('certificate_name');
    }

    /**
     * 手写数据
     * @return \app\model\SystemCertificate[]|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIsTjCertificateType()
    {
        return $this->where(['is_tj'=>1])->field('certificate_name,id,parent_id')->select()->toArray ();
    }

    /**
     * 获取父级名称
     * @param $id
     * @return mixed
     */
    public function getParentIdName($id)
    {
        return $this->where (['id'=>$id])->field('certificate_name,id,parent_id')->find();
    }

    /**
     * 获取证书信息
     * @param $id
     * @return array|null
     */
    public function getIdLikeFindModel($id, $where='')
    {
        $data = $this->where (['id'=>$id])->whereLike('certificate_name','%'.$where.'%')->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }
    public function getIdFindModel($id,$where='')
    {
        $data = $this->where (['parent_id'=>$id])->where('certificate_name',$where)->findOrEmpty ();
        if (!$data->isEmpty ()){
            $result = $data->toArray ();
            return $result['id'];
        }
        return null;
    }

    /**
     * 通过证书名称与父级ID查询当前证书id
     * @param $name
     * @param $parentId
     * @return mixed
     */
    public function getParentNameModel($parentId,$name)
    {
        return $this->where (['certificate_name'=>$name,'parent_id'=>$parentId])->value ('id');
    }


}